<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link
      rel="stylesheet"
      href="./general-styles-of-descendant-components.css"
    />
  </head>
  <body>
    <div id="root"></div>
    <script src="https://cdn.bootcdn.net/ajax/libs/react/17.0.1/umd/react.development.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/react-dom/17.0.1/umd/react-dom.development.min.js"></script>
    <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
    <script type="text/babel">
      const { Component, useEffect, useState } = React;
      const { unmountComponentAtNode } = ReactDOM;
      // 一上来0自增长
      class UseEffect extends Component {
        state = { count: 0 };
        componentDidMount() {
          this.timer = setInterval(() => {
            this.setState((state) => ({ count: state.count + 1 }));
          }, 1000);
        }
        unmount = () => {
          unmountComponentAtNode(document.getElementById("root"));
        };
        componentWillUnmount() {
          clearInterval(this.timer);
        }
        render() {
          return (
            <div className="bg">
              <span className="prominent">class</span>
              <h3>
                当前和为: <span className="prominent">{this.state.count}</span>
              </h3>
              <button onClick={this.unmount}>卸载组件</button>
            </div>
          );
        }
      }

      function UseEffect1() {
        const [count, setCount] = useState(0);
        useEffect(() => {
          const timer = setInterval(() => {
            setCount((count) => count + 1);
          }, 1000);
          return () => {
            // componentWillUnMount
            clearInterval(timer);
          };
        }, []);
        function unmount() {
          unmountComponentAtNode(document.getElementById("root"));
        }
        return (
          <div className="bg">
            <span className="prominent">useEffect</span>
            <h3>
              当前和为: <span className="prominent">{count}</span>
            </h3>
            <button onClick={unmount}>卸载组件</button>
          </div>
        );
      }

      let vdom = (
        <div>
          <UseEffect />
          <br />
          <UseEffect1 />
        </div>
      );
      ReactDOM.render(vdom, document.getElementById("root"));
    </script>
  </body>
</html>
